package com.guodi.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.AjaxResult;
import com.guodi.common.utils.PageData;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.model.entity.IdtIndicators;
import com.guodi.model.entity.IdtPredictedValue;
import com.guodi.model.service.IIdtIndicatorsService;
import com.guodi.model.service.IIdtPredictedValueService;
import com.guodi.model.service.IIdtReportValueService;
import com.guodi.model.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * @描述：预测值表控制类
 * @作者: 郑文彬
 * @日期: 2020-08-11
 */
@RestController
@RequestMapping("/IdtPredictedValue")
@Api(value = "API - IdtPredictedValueController", description = "预测值表")
public class IdtPredictedValueController extends BaseController {

    @Resource
    public IIdtPredictedValueService idtPredictedValueService;

    @Resource
    public IIdtIndicatorsService iIdtIndicatorsService;

    @Resource
    public IIdtReportValueService idtReportValueService;

    /**
     * @描述: 根据实体属性分页查询
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020-08-11
     **/
    @PostMapping("/listByEntity")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = true, dataType = "long", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "keyWord", value = "关键字搜索", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段（create_time,idtCode）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortWay", value = "排序方式(desc、asc)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "divisionCode", value = "行政区划代码", dataType = "string", paramType = "query")
    })
    @ApiOperation(value = " 根据实体属性分页查询")
    public AjaxResult listByEntity(@ApiIgnore Page page, String keyWord, String sort, String sortWay, String divisionCode){
        String deptId = this.getUser().getDeptId();
        if (StringUtils.isBlank(deptId)) {
            return new AjaxResult(StatusCode.ERROR_CODE_40005, "该用户所属部门不存在");
        }
        String orgId = deptId;
        if("create_time".equals(sort)){
            sort = "m.create_time";
        }
        IPage<IdtPredictedValue> iPage = idtPredictedValueService.listByKeyWord(page, keyWord, sort, sortWay, divisionCode, orgId);
        List<IdtPredictedValue> records = iPage.getRecords();

        if(records!=null && records.size()>0){
            //查询指标信息
            for (IdtPredictedValue idtPredictedValue: records) {
                //行政区划名称
                String divisionName = idtReportValueService.getNameByDivisionCode(idtPredictedValue.getDivisionCode());
                idtPredictedValue.setDivisionName(divisionName);
            }

        }
        return new AjaxResult(iPage);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020-08-11
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult findById(String id) {
        if(StringUtils.isBlank(id)){
            return new AjaxResult(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        IdtPredictedValue idtPredictedValue = idtPredictedValueService.getById(id);
        return new AjaxResult(idtPredictedValue);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020-08-11
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public AjaxResult save(IdtPredictedValue idtPredictedValue){
        prepareData(idtPredictedValue, true);
        idtPredictedValue.setId(UuidUtil.get32UUID());
        String deptId = this.getUser().getDeptId();
        if (StringUtils.isBlank(deptId)) {
            return new AjaxResult(StatusCode.ERROR_CODE_40005, "该用户所属部门不存在");
        }
        String orgId = deptId;
        idtPredictedValue.setOrgId(orgId);
        //相同指标不允许添加相同的监测年份、区域、模型类型
        LambdaQueryWrapper<IdtPredictedValue> queryWrapper = new QueryWrapper().lambda();
        queryWrapper.eq(IdtPredictedValue::getIndicatorsId, idtPredictedValue.getIndicatorsId());
        queryWrapper.eq(IdtPredictedValue::getYear, idtPredictedValue.getYear());
        queryWrapper.eq(IdtPredictedValue::getDivisionCode, idtPredictedValue.getDivisionCode());
        queryWrapper.eq(IdtPredictedValue::getModelType, idtPredictedValue.getModelType());
        List<IdtPredictedValue> list = idtPredictedValueService.list(queryWrapper);
        if (list.size() > 0) {
            return new AjaxResult(StatusCode.ERROR_CODE_40005, "该指标所处年份、行政区划预测值已存在");
        }
        idtPredictedValueService.save(idtPredictedValue);
        return new AjaxResult();
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020-08-11
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public AjaxResult update(IdtPredictedValue idtPredictedValue) {
        prepareData(idtPredictedValue, false);
        //相同指标不允许添加相同的监测年份、区域、模型类型
        LambdaQueryWrapper<IdtPredictedValue> queryWrapper = new QueryWrapper().lambda();
        queryWrapper.eq(IdtPredictedValue::getIndicatorsId, idtPredictedValue.getIndicatorsId());
        queryWrapper.eq(IdtPredictedValue::getYear, idtPredictedValue.getYear());
        queryWrapper.eq(IdtPredictedValue::getDivisionCode, idtPredictedValue.getDivisionCode());
        queryWrapper.ne(IdtPredictedValue::getId, idtPredictedValue.getId());
        queryWrapper.eq(IdtPredictedValue::getModelType, idtPredictedValue.getModelType());
        List<IdtPredictedValue> list = idtPredictedValueService.list(queryWrapper);
        if (list.size() > 0) {
            return new AjaxResult(StatusCode.ERROR_CODE_40005, "该指标所处年份、行政区划预测值已存在");
        }
        idtPredictedValueService.updateById(idtPredictedValue);
        return new AjaxResult();
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020-08-11
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult delete(String id){
        if (StringUtils.isBlank(id)) {
            new AjaxResult(StatusCode.ERROR_CODE_40005, "请输入id参数");
        }
        String[] idsAry = id.split(",");
        List<String> idList = Arrays.asList(idsAry);
        idtPredictedValueService.removeByIds(idList);
        return new AjaxResult();
    }

    /**
     * @描述:判断重复数据
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020/8/28 15:11
     **/
    @PostMapping("/isExistsPredicted")
    @ApiOperation(value = "判断重复数据")
    public AjaxResult isExistsPredicted(MultipartFile multipartFile) throws Exception {
        String deptId = this.getUser().getDeptId();
        if (StringUtils.isBlank(deptId)) {
            return new AjaxResult(StatusCode.ERROR_CODE_40005, "该用户所属部门不存在");
        }
        if (multipartFile.isEmpty()) {
            new AjaxResult(StatusCode.ERROR_CODE_40005, "导入文件不能为空！");
        }
        // 根据上传MultipartFile对象获取excel数据集
        List<List<String>> excelList = ExcelUtil.getExcelData(multipartFile);
        if (excelList == null || excelList.size() <= 1) {
            new AjaxResult(StatusCode.ERROR_CODE_40005, "导入文件数据为空！");
        }
        // 存储新增、更新指标集合
        List<IdtPredictedValue> repeatingData = new ArrayList<>();
        for (int i = 1; i < excelList.size(); i++) {
            List<String> dataList = excelList.get(i);
            String idtCode = dataList.get(0);
            LambdaQueryWrapper<IdtIndicators> queryWrapper = new QueryWrapper().lambda();
            queryWrapper.eq(IdtIndicators::getCode, idtCode);
            IdtIndicators idtIndicators = iIdtIndicatorsService.getOne(queryWrapper);
            IdtPredictedValue idtPredictedValue = new IdtPredictedValue();
            String year = dataList.get(5).indexOf(".") >= 0 ? dataList.get(5).substring(0, dataList.get(5).indexOf(".")) : dataList.get(5);
            idtPredictedValue.setYear(Integer.parseInt(year));
            idtPredictedValue.setIdtCode(idtCode);
            if(StringUtils.isNotBlank(dataList.get(4))){
                //可能读成数字了，带小数，去掉小数
                if(dataList.get(4).indexOf(".") != -1){
                    String[] str =  dataList.get(4).split("\\.");
                    idtPredictedValue.setDivisionCode(str[0]);
                }else{
                    idtPredictedValue.setDivisionCode(dataList.get(4));
                }
            }
            idtPredictedValue.setPredictedValue(dataList.get(6));
            idtPredictedValue.setModelType(dataList.get(7));
            idtPredictedValue.setId(UuidUtil.get32UUID());
            idtPredictedValue.setOrgId(deptId);
            idtPredictedValue.setDivisionName(dataList.get(3));
            if (idtIndicators != null) {
                //判断是否存在相同指标id与行政区划编码与相同年份
                LambdaQueryWrapper<IdtPredictedValue> queryWrapper1 = new QueryWrapper().lambda();
                queryWrapper1.eq(IdtPredictedValue::getYear, idtPredictedValue.getYear());
                queryWrapper1.eq(IdtPredictedValue::getDivisionCode, idtPredictedValue.getDivisionCode());
                queryWrapper1.eq(IdtPredictedValue::getIndicatorsId, idtIndicators.getId());
                queryWrapper1.eq(IdtPredictedValue::getModelType,dataList.get(7));
                List<IdtPredictedValue> sourceIdtMonitorValueList = idtPredictedValueService.list(queryWrapper1);
                if (sourceIdtMonitorValueList.size() > 0) {
                    repeatingData.add(idtPredictedValue);
                }
            }
        }
        String resultCodes = "";
        for (IdtPredictedValue obj : repeatingData) {
            if (!resultCodes.contains(obj.getIdtCode())) {
                resultCodes += StringUtils.isBlank(resultCodes) ? obj.getIdtCode() : "," + obj.getIdtCode();
            }
        }
        return new AjaxResult(resultCodes);
    }
    /**
     * @描述:导入Excel
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020/8/11 14:45
     **/
    @PostMapping("/importExcel")
    @ApiOperation(value = "导入Excel")
    public AjaxResult importExcel(MultipartFile multipartFile, Boolean isUpdate) throws Exception {
        String deptId = this.getUser().getDeptId();
        if (StringUtils.isBlank(deptId)) {
            return new AjaxResult(StatusCode.ERROR_CODE_40005, "该用户所属部门不存在");
        }
        if (multipartFile.isEmpty()) {
            new AjaxResult(StatusCode.ERROR_CODE_40005, "导入文件不能为空！");
        }
        // 根据上传MultipartFile对象获取excel数据集
        List<List<String>> excelList = ExcelUtil.getExcelData(multipartFile);
        if (excelList == null || excelList.size() <= 1) {
            new AjaxResult(StatusCode.ERROR_CODE_40005, "导入文件数据为空！");
        }
        // 存储新增、更新指标集合
        List<IdtPredictedValue> saveUpdateList = new ArrayList<>();
        for (int i = 1; i < excelList.size(); i++) {
            List<String> dataList = excelList.get(i);
            String idtCode = dataList.get(0);
            //String idtName = dataList.get(1);
            //String idtUnit = dataList.get(2);
            LambdaQueryWrapper<IdtIndicators> queryWrapper = new QueryWrapper().lambda();
            queryWrapper.eq(IdtIndicators::getCode, idtCode);
            //queryWrapper.eq(IdtIndicators::getName,idtName);
            //queryWrapper.eq(IdtIndicators::getUnit,idtUnit);
            IdtIndicators idtIndicators = iIdtIndicatorsService.getOne(queryWrapper);
            IdtPredictedValue idtPredictedValue = new IdtPredictedValue();
            String year = dataList.get(5).indexOf(".") >= 0 ? dataList.get(5).substring(0, dataList.get(5).indexOf(".")) : dataList.get(5);
            idtPredictedValue.setYear(Integer.parseInt(year));
            if(StringUtils.isNotBlank(dataList.get(4))){
                //可能读成数字了，带小数，去掉小数
                if(dataList.get(4).indexOf(".") != -1){
                    String[] str =  dataList.get(4).split("\\.");
                    idtPredictedValue.setDivisionCode(str[0]);
                }else{
                    idtPredictedValue.setDivisionCode(dataList.get(4));
                }
            }
            idtPredictedValue.setPredictedValue(dataList.get(6));
            idtPredictedValue.setModelType(dataList.get(7));
            //dtMonitorValue.setWarnStatus(dataList.get(4));
            idtPredictedValue.setId(UuidUtil.get32UUID());
            idtPredictedValue.setOrgId(deptId);
            idtPredictedValue.setDivisionName(dataList.get(3));
            if (idtIndicators != null) {
                //判断是否存在相同指标id与行政区划编码与相同年份
                LambdaQueryWrapper<IdtPredictedValue> queryWrapper1 = new QueryWrapper().lambda();
                queryWrapper1.eq(IdtPredictedValue::getYear, idtPredictedValue.getYear());
                queryWrapper1.eq(IdtPredictedValue::getDivisionCode, idtPredictedValue.getDivisionCode());
                queryWrapper1.eq(IdtPredictedValue::getIndicatorsId, idtIndicators.getId());
                queryWrapper1.eq(IdtPredictedValue::getModelType,dataList.get(7));
                List<IdtPredictedValue> sourceIdtMonitorValueList = idtPredictedValueService.list(queryWrapper1);
                if (sourceIdtMonitorValueList.size() > 0) {
                    if(isUpdate != null && isUpdate){
                        IdtPredictedValue predictedValue = sourceIdtMonitorValueList.get(0);
                        predictedValue.setPredictedValue(idtPredictedValue.getPredictedValue());
                        idtPredictedValue = predictedValue;
                    }else{
                        continue;
                    }
                }
                idtPredictedValue.setIndicatorsId(idtIndicators.getId());
                idtPredictedValue.setIdtCode(idtIndicators.getCode());
                idtPredictedValue.setIdtName(idtIndicators.getName());
                idtPredictedValue.setIdtUnit(idtIndicators.getUnit());
                prepareData(idtPredictedValue, true);
                saveUpdateList.add(idtPredictedValue);
            }
        }
        if (saveUpdateList.size() > 0) {
            // 批量保存更新
            idtPredictedValueService.saveOrUpdateBatch(saveUpdateList);
        }
        return new AjaxResult(saveUpdateList);
    }

    /**
     * @描述:导出Excel
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020/8/11 16:20
     **/
    @PostMapping("/exportExcel")
    @ApiOperation(value = "导出指标Excel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exportType", value = "导出类型,传入all默认导出全部,否则根据,分隔导出", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ids", value = "导出指标id集合", required = false, dataType = "string", paramType = "query")
    })
    public void exportExcel(String exportType, String ids) {
        String deptId = this.getUser().getDeptId();
        if (StringUtils.isBlank(deptId)) {
            return;
        }
        String orgId = deptId;
        List<IdtPredictedValue> list;
        //查询指标信息子项信息
        List<IdtIndicators> idtIndicatorsList;
        List<String> stringIdList = new ArrayList<>();
        if ("all".equals(exportType)) {
            // 导出所有指标
            LambdaQueryWrapper<IdtPredictedValue> queryWrapper = new QueryWrapper().lambda();
            queryWrapper.eq(IdtPredictedValue::getOrgId, orgId);
            list = idtPredictedValueService.list(queryWrapper);
        } else {
            List<String> idsList = Arrays.asList(ids.split(","));
            // 导出选中指标
            list = (List<IdtPredictedValue>) idtPredictedValueService.listByIds(idsList);

        }
        for (int z = 0; z < list.size(); z++) {
            stringIdList.add(list.get(z).getIndicatorsId());
        }
        //指标子项信息
        idtIndicatorsList = (List<IdtIndicators>) iIdtIndicatorsService.list();
        //赋值指标相关子项信息
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < idtIndicatorsList.size(); j++) {
                if (list.get(i).getIndicatorsId().equals(idtIndicatorsList.get(j).getId())) {
                    IdtIndicators idtIndicators = idtIndicatorsList.get(j);
                    list.get(i).setIdtUnit(idtIndicators.getUnit());
                    list.get(i).setIdtName(idtIndicators.getName());
                    list.get(i).setIdtCode(idtIndicators.getCode());
                    String divisionName = idtReportValueService.getNameByDivisionCode(list.get(i).getDivisionCode());
                    list.get(i).setDivisionName(divisionName);
                }
            }
        }
        List<Object[]> dataList = new ArrayList<Object[]>();
        for (int i = 0; i < list.size(); i++) {
            IdtPredictedValue idtMonitorValue = list.get(i);
            Object[] datas = new Object[8];
            datas[0] = idtMonitorValue.getIdtCode();
            datas[1] = idtMonitorValue.getIdtName();
            datas[2] = idtMonitorValue.getIdtUnit();
            datas[3] = idtMonitorValue.getDivisionName();
            datas[4] = idtMonitorValue.getDivisionCode();
            datas[5] = idtMonitorValue.getYear();
            datas[6] = idtMonitorValue.getPredictedValue();
            datas[7] = idtMonitorValue.getModelType();
            dataList.add(datas);
        }
        // 导出Excel
        String[] titles = {"指标编号", "指标名称", "单位", "行政区划名称", "行政区划代码", "年份", "监测值", "预测类型"};
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        ExcelUtil.exportExcel(response, "监测指标", "监测指标信息", titles, dataList);
    }

    /**
     * @描述:查询某城市各个年份的预测值
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020/8/12 15:37
     **/
    @PostMapping("/listPredictedValByCity")
    @ApiOperation(value = "查询某城市各个年份的预测值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "指标名称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "years", value = "多个年份按逗号隔开", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "区域编码", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "modelType", value = "预测模型类型", required = false, dataType = "string", paramType = "query")
    })
    public AjaxResult listPredictedValByCity(String name, String years, String areaCode, String modelType) {
        //查询指标信息
        LambdaQueryWrapper<IdtIndicators> lambda1 = new QueryWrapper().lambda();
        lambda1.eq(IdtIndicators::getName,name);
        IdtIndicators one = iIdtIndicatorsService.getOne(lambda1);
        String indicatorsId = one.getId();
        String[] split = years.split(",");
        List<IdtPredictedValue> list = new ArrayList<>();
        for (String year:split) {
            LambdaQueryWrapper<IdtPredictedValue> lambda2 = new QueryWrapper().lambda();
            lambda2.eq(IdtPredictedValue::getYear,year);
            lambda2.eq(IdtPredictedValue::getDivisionCode,areaCode);
            lambda2.eq(IdtPredictedValue::getIndicatorsId,indicatorsId);
            lambda2.eq(IdtPredictedValue::getModelType,modelType);
            IdtPredictedValue predictedValue = idtPredictedValueService.getOne(lambda2);
            if(predictedValue!=null){
                //城市名称
                String divisionName = idtReportValueService.getNameByDivisionCode(predictedValue.getDivisionCode());
                predictedValue.setDivisionName(divisionName);
                predictedValue.setIdtCode(one.getCode());
                predictedValue.setIdtName(one.getName());
                predictedValue.setIdtUnit(one.getUnit());
                list.add(predictedValue);
            }else {
                IdtPredictedValue idtPredictedValue = new IdtPredictedValue();
                idtPredictedValue.setYear(Integer.parseInt(year));
                list.add(predictedValue);
            }
        }
        return  new AjaxResult(list);
    }
    /**
     * @描述:查询父城市下各子城市各个年份的预测值
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020/8/12 15:37
     **/
    @PostMapping("/listPredictedValByParentCity")
    @ApiOperation(value = "查询父城市下各子城市各个年份的预测值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "指标名称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "years", value = "多个年份按逗号隔开", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "区域编码", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "modelType", value = "预测模型类型", required = false, dataType = "string", paramType = "query")
    })
    public AjaxResult listPredictedValByParentCity(String name, String years, String areaCode, String modelType) {
        HashMap hashMap = new HashMap();
        //查询指标信息
        LambdaQueryWrapper<IdtIndicators> lambda1 = new QueryWrapper().lambda();
        lambda1.eq(IdtIndicators::getName,name);
        IdtIndicators one = iIdtIndicatorsService.getOne(lambda1);
        String indicatorsId = one.getId();
        //查询子城市
        PageData pageData = new PageData();
        pageData.put("parent_area_code",areaCode);
        List<PageData> pdList = iIdtIndicatorsService.listDivisionByEntity(pageData);
        String[] split = years.split(",");
        for (PageData pd: pdList) {
            String code = pd.getString("code");
            String areaName = pd.getString("name");
            List<IdtPredictedValue> list = new ArrayList<>();
            for (String year:split) {
                LambdaQueryWrapper<IdtPredictedValue> lambda2 = new QueryWrapper().lambda();
                lambda2.eq(IdtPredictedValue::getYear,year);
                lambda2.eq(IdtPredictedValue::getDivisionCode,code);
                lambda2.eq(IdtPredictedValue::getIndicatorsId,indicatorsId);
                lambda2.eq(IdtPredictedValue::getModelType,modelType);
                IdtPredictedValue predictedValue = idtPredictedValueService.getOne(lambda2);
                if(predictedValue!=null){
                    predictedValue.setDivisionName(areaName);
                    predictedValue.setIdtCode(one.getCode());
                    predictedValue.setIdtName(one.getName());
                    predictedValue.setIdtUnit(one.getUnit());
                    list.add(predictedValue);
                }else {
                    IdtPredictedValue idtPredictedValue = new IdtPredictedValue();
                    idtPredictedValue.setYear(Integer.parseInt(year));
                    idtPredictedValue.setDivisionName(areaName);
                    idtPredictedValue.setDivisionCode(code);
                    list.add(predictedValue);
                }
            }
            hashMap.put(areaName,list);
        }
        return  new AjaxResult(hashMap);
    }
    private void prepareData(IdtPredictedValue idtPredictedValue, boolean isSave) {
        if(isSave){
            idtPredictedValue.setCreator(this.getUser().getUserId().toString());
            idtPredictedValue.setCreateTime(new Date());
            //防止误修改
            idtPredictedValue.setModifier(null);
            idtPredictedValue.setModifyTime(null);
        }else{
            idtPredictedValue.setModifier(this.getUser().getUserId().toString());
            idtPredictedValue.setModifyTime(new Date());
            //防止误修改
            idtPredictedValue.setCreator(null);
            idtPredictedValue.setCreateTime(null);
        }
    }
}
